<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataFormValue" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item style="width: 50%" label="所属杆塔">
            <el-input v-model="dataForm.stationName" placeholder="所属杆塔" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通道编号">
            <el-input v-model="dataForm.channelNo" placeholder="通道编号" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="间隔时间" prop="takeTime">
            <el-time-select
                v-model="dataFormValue.takeTime"
                style="width: 240px"
                class="mr-4"
                placeholder="间隔时间"
                start="00:00"
                step="00:15"
                end="23:59"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="工作时间" required>
            <el-form-item prop="startTime">
              <el-time-select
                  v-model="dataFormValue.startTime"
                  style="width: 240px"
                  :max-time="dataFormValue.endTime"
                  class="mr-4"
                  placeholder="开始工作时间"
                  start="00:00"
                  step="00:15"
                  end="23:59"
              />
            </el-form-item>

            <span style="margin: 0 20px">—</span>

            <el-form-item prop="endTime">
              <el-time-select
                  v-model="dataFormValue.endTime"
                  style="width: 240px"
                  :min-time="dataFormValue.startTime"
                  placeholder="结束工作时间"
                  start="00:00"
                  step="00:15"
                  end="23:59"
              />
            </el-form-item>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!--    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"-->
    <!--             label-width="120px">-->
    <!--      <el-form-item label="创建时间" prop="createDate">-->
    <!--        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="" prop="stationId">-->
    <!--        <el-input v-model="dataForm.stationId" placeholder=""></el-input>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="帧序列号" prop="frameNo">-->
    <!--        <el-input v-model="dataForm.frameNo" placeholder="帧序列号"></el-input>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="" prop="flag">-->
    <!--        <el-input v-model="dataForm.flag" placeholder=""></el-input>-->
    <!--      </el-form-item>-->
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">下发命令</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {useI18n} from "vue-i18n";
import {ElMessage, FormInstance} from "element-plus";

const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  createDate: "",
  id: "",
  stationName: "",
  frameNo: "",
  flag: "",
  channelNo: "",
  hour1: 0,
  minute1: 1,
  hour2: 0,
  minute2: 1,
  hour3: 0,
  minute3: 1,
});

function padStartTime(hour: Number, minute: Number) {
  if (hour) {
    return hour.toString().padStart(2, '0') + ":" + minute.toString().padStart(2, '0');
  } else {
    return "00:00"
  }
}

const dataFormValue = ref({
  takeTime: "",
  startTime: "",
  endTime: ""
})

const rules = ref({
  takeTime: [
    {
      required: true,
      message: '抓拍时间',
      trigger: 'change',
    },
  ],
  startTime: [
    {
      required: true,
      message: '工作开始时间',
      trigger: 'change',
    },
  ],
  endTime: [
    {
      required: true,
      message: '工作结束时间',
      trigger: 'change',
    },
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/KxImageSettings/kximagesettings/" + id).then((res) => {
    Object.assign(dataForm, res.data);
    if (res.data.hour1) {
      dataFormValue.value.takeTime = padStartTime(res.data.hour1, res.data.minute1);
    }
    if (res.data.hour2) {
      dataFormValue.value.startTime = padStartTime(res.data.hour2, res.data.minute2);
    }
    if (res.data.hour3) {
      dataFormValue.value.endTime = padStartTime(res.data.hour3, res.data.minute3);
    }
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    const newTakeTime: any = dataFormValue.value.takeTime.split(':');
    const newStartTime: any = dataFormValue.value.startTime.split(':');
    const newEndTime: any = dataFormValue.value.endTime.split(':');
    dataForm.hour1 = parseInt(newTakeTime[0], 10);
    dataForm.minute1 = parseInt(newTakeTime[1], 10);
    dataForm.hour2 = parseInt(newStartTime[0], 10);
    dataForm.minute2 = parseInt(newStartTime[1], 10);
    dataForm.hour3 = parseInt(newEndTime[0], 10);
    dataForm.minute3 = parseInt(newEndTime[1], 10);
    (!dataForm.id ? baseService.post : baseService.put)("/KxImageSettings/kximagesettings", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
